﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using TekaMUD.Engine.Commands;

using System.Threading;
using TekaMUD.Engine;
using TekaMUD.Engine.Framework;

namespace TekaMUD.Engine.Workers
{
	[Obsolete("Change the queue to a priority queue, throttle player input")]
	public class WorkerThreadCommandDispatcher : WorkerThread
	{
		private Queue<Command> _queue = new Queue<Command>();

		public WorkerThreadCommandDispatcher() : base("Command Dispatcher")
		{
		}

		protected override void Run()
		{
			while (!Terminated)
			{
				Command cmd = null;

				try
				{
					// The order of the following 2 statements is relevant.
					cmd = CommandQueue.DequeueCommand();
					Locks.StorageLock.EnterReadLock();

					cmd.Execute();
				}
				catch (Exception ex)
				{
					Logger.LogError("Error parsing command from channel " + (cmd != null ? cmd.Channel.ToString() : "<NULL>"));
				}
				finally
				{
					Locks.StorageLock.ExitReadLock();
				}
			}

			Logger.LogInfo("[Thread] Command Dispatcher terminated.");
		}
	}
}
